package com.sanyou.controller;

import com.sanyou.pojo.Menu;
import com.sanyou.service.MenuService;
import com.sanyou.utils.JSONResult;
import com.sanyou.utils.PagedResult;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sanyou.controller.BasicController.PAGE_SIZE;

@Api(value = "菜单相关的接口",tags = {"菜单相关的Controller"})
@CrossOrigin
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/getMenuItems")
    public JSONResult getMenuItems(String userId){
        return JSONResult.ok(menuService.getMenuItems(userId));
    }

    @GetMapping("/getParentMenu")
    public JSONResult getParentMenu(){
        return JSONResult.ok(menuService.getParentMenu());
    }


    @GetMapping("/query")
    public JSONResult query(String query, Integer page, Integer pageSize){

        if(page == null)
            page = 1;

        if(pageSize == null)
            pageSize = PAGE_SIZE;

        PagedResult menuList = menuService.query(query, page, pageSize);

        return JSONResult.ok(menuList);
    }

    @PostMapping("/addMenu")
    public JSONResult addMenu(@RequestBody Menu menu){
        if(menu == null || StringUtils.isBlank(menu.getText()))
            return JSONResult.errorMsg("菜单名称不能为空!");

        menu.setParent(1);
        menuService.addMenu(menu);
        return JSONResult.ok();
    }

    @PostMapping("/addSubMenu")
    public JSONResult addSubMenu(@RequestBody Menu menu){
        if(menu == null || StringUtils.isBlank(menu.getName()))
            return JSONResult.errorMsg("菜单编码不能为空!");

        if(StringUtils.isBlank(menu.getText()))
            return JSONResult.errorMsg("菜单名称不能为空!");

        if(menu.getParentId() == null)
            return JSONResult.errorMsg("父菜单不存在!");

        menu.setParent(0);
        menuService.addSubMenu(menu);
        return JSONResult.ok();
    }

    @PostMapping("/deleteMenu")
    public JSONResult deleteMenu(@RequestBody Menu menu){
        if(menu == null || menu.getId() == null)
            return JSONResult.errorMsg("菜单不存在!");

        menuService.deleteMenu(menu);
        return JSONResult.ok();
    }

    @PostMapping("/updateMenu")
    public JSONResult updateMenu(@RequestBody Menu menu){
        if(menu == null || menu.getId() == null)
            return JSONResult.errorMsg("菜单不存在!");

        if(StringUtils.isBlank(menu.getText()))
            return JSONResult.errorMsg("菜单名称不能为空!");

        Menu updateMenu = new Menu();
        updateMenu.setId(menu.getId());
        updateMenu.setText(menu.getText());
        updateMenu.setName(menu.getName());

        menuService.updateMenu(updateMenu);

        return JSONResult.ok();
    }

    @GetMapping("/querySubMenu")
    public JSONResult querySubMenu(String parentId){
        if(StringUtils.isBlank(parentId))
            return JSONResult.errorMsg("菜单不存在!");

        List<Menu> subMenuList = menuService.querySubMenu(parentId);

        return JSONResult.ok(subMenuList);
    }
}
